package utility.files;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Lines {
	private List<String> _lines;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((_lines == null) ? 0 : _lines.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Lines))
			return false;
		Lines other = (Lines) obj;
		if (_lines == null) {
			if (other._lines != null)
				return false;
		} else if (!_lines.equals(other._lines))
			return false;
		return true;
	}

	public Lines(){
		_lines = new ArrayList<String>();
	}

	public void putLine(String aLine) {
		_lines.add(aLine);
	}
	
	public void put(String aString){
		put(aString,"",false,false);
	}
	
	public void put(String aString, String separator, boolean atBegin, boolean atEnd){
		String toInsert = aString;
		if (atBegin) toInsert = separator+toInsert;
		if (atEnd) toInsert = toInsert+separator;
		
		int lastIndex = _lines.size()-1;
		if(lastIndex>=0){
			toInsert = _lines.get(lastIndex)+toInsert;
			_lines.remove(lastIndex);
		}
		_lines.add(toInsert);
	}

	

	public String asText() {
		return asLine("\n",false);
	}
	
	public List<String> asList(){
		return _lines;
	}

	public String asLine(String separator, boolean atEnd) {
		StringBuffer result = new StringBuffer();
		for (String each : _lines) {
			result.append(each);
			result.append(separator);
		}
		
		if(!atEnd&&result.length()>=separator.length())
			return result.substring(0, result.length()-separator.length());
		
		return result.toString();
	}

	public static Lines from(File aFile) {
		TextFile file = new TextFile(aFile,true);
		return file.asLines();
	}

	public int size() {
		return _lines.size();
	}

	public void clean() {
		_lines.clear();
	}
	
	public String extractFirstLineStartingWith(String fieldName, boolean removeField) {
		for (String each : _lines) {
			if(each.startsWith(fieldName)) {
				if (removeField) return each.substring(fieldName.length());
				else return each;
			}
		}
		return "";
	}

	public static Lines from(String text) {
		return from(text, System.getProperty("line.separator"));
	}

	public static Lines from(String text, String lineSeparator) {
		Lines lines = new Lines();
		int separatorLength = lineSeparator.length();
		for(int currentIndex = 0; currentIndex!=-1;){
			String line = new String();
			int newIndex = text.indexOf(lineSeparator, currentIndex+1);
			if (currentIndex>0) currentIndex = currentIndex+separatorLength;
			if (newIndex!=-1) line = text.substring(currentIndex, newIndex);
			else line = text.substring(currentIndex);
			lines.putLine(line);
			currentIndex = newIndex;
		}
		
		return lines;
	}

	public void mergeWith(Lines lines) {
		_lines.addAll(lines._lines);
	}

}
